/*
 * @lc app=leetcode.cn id=547 lang=java
 *
 * [547] 省份数量
 */

// @lc code=start
class Solution {
    public int findCircleNum(int[][] isConnected) {
        int length = isConnected.length;
        int count = 0;
        boolean[] visited = new boolean[length];

        for(int i = 0; i < length; i++) {
            if (!visited[i]) {
                dfs(isConnected, visited, i);
                count++;
                
            }
        }
        return count;
    }

    private void dfs(int[][] isConnected, boolean[] visited, int i) {
        
        for(int j = 0; j < isConnected.length; j++) {
            if(isConnected[i][j] == 1 && !visited[j]) {
                visited[j] = true;
                dfs(isConnected, visited, j);
            }
        }
    }
}
// @lc code=end

